//
//  ViewController.m
//  可以移动的colletionView
//
//  Created by mafeng on 16/8/11.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) NSMutableArray *array;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    layout.itemSize = CGSizeMake(80, 80);
    [self.view addSubview:_collection];
    _collection.delegate = self;
    _collection.dataSource = self;
    
    
    [_collection registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"reuse"];
    self.array = [NSMutableArray array];
    for (int i = 0; i < 50; i++) {
        [_array addObject:[NSString stringWithFormat:@"%d", i]];
    }
    // 添加手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [_collection addGestureRecognizer:longPress];
}

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    
    switch (longPress.state) {
        case UIGestureRecognizerStateBegan:
            // 在此要加大括号, 不然不能创建新的变量
        {
            NSIndexPath *beginIndexPath = [_collection indexPathForItemAtPoint:[longPress locationInView:self.collection]];
            [self.collection beginInteractiveMovementForItemAtIndexPath:beginIndexPath];
            break;
        }
        case UIGestureRecognizerStateChanged:
            [self.collection updateInteractiveMovementTargetPosition:[longPress locationInView:_collection]];
            break;
        case UIGestureRecognizerStateEnded:
            [_collection endInteractiveMovement];
            break;
        default:
            break;
    }
    
}
// 是否可以移动
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// 移动时触发该方法, 在此做 数据源 处理
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    NSString *sourceString = _array[sourceIndexPath.row];
    [_array removeObjectAtIndex:sourceIndexPath.row];
    [_array insertObject:sourceString atIndex:destinationIndexPath.row];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _array.count;
}
// 点击item 消失时会有动画(系统封装的)
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // 更新数据源
    [_array removeObjectAtIndex:indexPath.row];
    // 更新collectionView时有动画
    [_collection performBatchUpdates:^{
        [_collection deleteItemsAtIndexPaths:@[indexPath]];
    } completion:nil];
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
    cell.label.text = _array[indexPath.row];
    return cell;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
